在相同的檢索結果 + prompt 下
分別使用:
比較:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
def generate_answer(model_name, context, query):
print(f"\n===== 測試模型:{model_name} =====")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
# 英文 prompt
prompt = f"""
You are a helpful assistant.
Here is the FAQ:
{context}
Question: {query}
Answer in English, short and clear:
"""
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
outputs = model.generate(**inputs, max_new_tokens=200)
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
if answer.strip() == "":
print("⚠️ 模型輸出為空")
else:
print("✅ 回答內容:", answer)
return answer
# 測試資料
context = "If the product has defects, please take a photo and go to the customer service center to fill out a form. We will handle replacement or refund as soon as possible."
query = "What should I do if my product has defects?"
# === 測試 flan-t5-base ===
ans_base = generate_answer("google/flan-t5-base", context, query)
# === 測試 flan-t5-small ===
ans_small = generate_answer("google/flan-t5-small", context, query)
結果 :
===== 測試模型:google/flan-t5-base =====
✅ 回答內容: Take a photo and go to the customer service center to fill out a form.
===== 測試模型:google/flan-t5-small =====
✅ 回答內容: Take a photo and go to the customer service center to fill out a form. We will handle replacement or refund as soon as possible.
比較 :